package com.dp.client.views.reportesCharts;

import org.moxieapps.gwt.highcharts.client.Chart;
import org.moxieapps.gwt.highcharts.client.Legend;
import org.moxieapps.gwt.highcharts.client.Point;
import org.moxieapps.gwt.highcharts.client.ToolTip;
import org.moxieapps.gwt.highcharts.client.ToolTipData;
import org.moxieapps.gwt.highcharts.client.ToolTipFormatter;
import org.moxieapps.gwt.highcharts.client.labels.DataLabelsData;
import org.moxieapps.gwt.highcharts.client.labels.DataLabelsFormatter;
import org.moxieapps.gwt.highcharts.client.labels.PieDataLabels;
import org.moxieapps.gwt.highcharts.client.plotOptions.PiePlotOptions;
import org.moxieapps.gwt.highcharts.client.Series;

public class CrearBackPieChart {
	private Chart chart;
	private Point [] points;
	private String nombre;
	private String tipo;
	
	// tipo "double" "entero"
	public CrearBackPieChart(Point [] points, String nombre,String tipo){
		this.points=points;
		this.nombre=nombre;
		this.tipo=tipo;
		init();
	}
	public void init(){
		chart = new Chart()  
	    .setType(Series.Type.PIE)  
	    .setChartTitleText(nombre)  
	    .setPlotBackgroundColor((String) null)  
	    .setPlotBorderWidth(null)  
	    .setPlotShadow(false)  
	    .setPiePlotOptions(new PiePlotOptions()  
	        .setAllowPointSelect(true)  
	          
	        .setPieDataLabels(new PieDataLabels()  
	            .setConnectorColor("#000000")  
	            .setEnabled(true)  
	            .setColor("#000000")  
	            .setFormatter(new DataLabelsFormatter() {  
	                public String format(DataLabelsData dataLabelsData) {  
	                    if (tipo.compareTo("double")==0)
                    		return "<b>" + dataLabelsData.getPointName() + "</b>: " + dataLabelsData.getYAsDouble() + " %";
                    	else
                    		return "<b>" + dataLabelsData.getPointName() + "</b>: " + dataLabelsData.getYAsLong();
	                }  
	            })  
	        )  
	    )  
	    .setLegend(new Legend()  
	        .setLayout(Legend.Layout.VERTICAL)  
	        .setAlign(Legend.Align.RIGHT)  
	        .setVerticalAlign(Legend.VerticalAlign.TOP)  
	        .setX(-100)  
	        .setY(100)  
	        .setFloating(true)  
	        .setBorderWidth(1)  
	        .setBackgroundColor("#FFFFFF")  
	        .setShadow(true)  
	    )  
	    .setToolTip(new ToolTip()  
                .setFormatter(new ToolTipFormatter() {  
                    public String format(ToolTipData toolTipData) {  
                    	if (tipo.compareTo("double")==0)
                    		return "<b>" + toolTipData.getPointName() + "</b>: " + toolTipData.getYAsDouble() + " %";
                    	else
                    		return "<b>" + toolTipData.getPointName() + "</b>: " + toolTipData.getYAsLong();
                        
                    }  
                })  
        );  
	
		chart.addSeries(chart.createSeries()  
		    .setName("Browser share")  
		    .setPoints(points)  
		);
	}
	public Chart getChart(){
		   
		return chart;
	}
	public String format(ToolTipData toolTipData) {  
    	if (tipo.compareTo("double")==0)
    		return "<b>" + toolTipData.getPointName() + "</b>: " + toolTipData.getYAsDouble() + " %";
    	else
    		return "<b>" + toolTipData.getPointName() + "</b>: " + toolTipData.getYAsLong();
        
    }
}
